﻿using Dapper.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using YouWei.Api.Common;
using YouWei.Api.Models;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using YouWei.Infrastructure.Service;

namespace YouWei.Api.Controllers
{
    /// <summary>
    /// 基础保险数据
    /// </summary>
    public class InsureALLController : BaseApiController
    {
        private InsureALLService _service = new InsureALLService();
        private InsureRecordService _recordService = new InsureRecordService();

        /// <summary>
        /// 根据车牌号码获取保险信息
        /// </summary>
        /// <param name="PlateNo"></param>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<InsureALL> GetInsureAllByPlateNo(string PlateNo, int UserId)
        {
            ResultData<InsureALL> result = ResultData<InsureALL>.Error("未查询到信息");
            if (string.IsNullOrWhiteSpace(PlateNo))
            {
                result.message = "缺少必要的参数PlateNo";
                return result;
            }

            //同一个用户 一天查询超过3次，就返回空 防止攻击
            List<InsureRecord> records = _recordService.GetByUser(UserId);
            _recordService.Insert(new InsureRecord()
            {
                CreateDate = DateTime.Now,
                PlateNo = PlateNo,
                UserID = UserId
            });
            if (records.Count > 2)
            {
                return result;
            }

            var data = _service.GetByPlateNo(PlateNo);
            if (data != null)
            {
                data.VIN = "";
                data.ZZRQ = DefultTime;
                data.SXRQ = DefultTime;
                result.status = 0;
                result.message = "查询成功";
                result.data = data;
            }
            return result;
        }
    }
}
